//
//  NewFeatureCollectionViewController.swift
//  0730Microblog
//
//  Created by ding on 15/8/2.
//  Copyright © 2015年 ding. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let cellCount = 4

class NewFeatureCollectionViewController: UICollectionViewController {
    
    private let flowLayout = NewFeatrueCellFlowLayout()
    
    init() {
        super.init(collectionViewLayout: flowLayout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.backgroundColor = UIColor.lightGrayColor()

        // Register cell classes
        self.collectionView!.registerClass(NewFeatrueCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return cellCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatrueCell
    
        cell.imgIndex = indexPath.item
        
        return cell
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let currentCellIndexPath = collectionView.indexPathsForVisibleItems().last
//        print(currentCellIndexPath?.item)
        let currentCell = collectionView.cellForItemAtIndexPath(currentCellIndexPath!)
        (currentCellIndexPath!.item == cellCount - 1) ? (currentCell as! NewFeatrueCell).startedAnimation() : doNothing()
    }
    
    private func doNothing() {
        
    }

}

class NewFeatrueCell: UICollectionViewCell {
    
    func startedAnimation() {
        startExperienceBtn.transform = CGAffineTransformScale(startExperienceBtn.transform, 0, 0)
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 8, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startExperienceBtn.transform = CGAffineTransformIdentity
            }, completion: nil)
    }
    
    private var imgIndex: Int = 0 {
        didSet {
            iconImgView.image = UIImage(named: "new_feature_\(imgIndex + 1)")
            startExperienceBtn.hidden = (imgIndex == cellCount - 1) ? false : true
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupCollectionCell()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupCollectionCell() {
        contentView.addSubview(iconImgView)
        contentView.addSubview(startExperienceBtn)
        
        // 设置约束
        iconImgView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0))
        
        startExperienceBtn.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: startExperienceBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startExperienceBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -contentView.frame.height * CGFloat(0.35)))
        
    }
    
    // 懒加载控件
    private lazy var iconImgView = UIImageView()
    
    private lazy var startExperienceBtn: UIButton = {
       let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.sizeToFit()
        btn.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    func startBtnClick() {
        print("startBtnClick~~~~~~~~~~~~")
        NSNotificationCenter.defaultCenter().postNotificationName(DINGSwitchRootViewControllerNotification, object: true)
    }
}

private class NewFeatrueCellFlowLayout: UICollectionViewFlowLayout {
    private override func prepareLayout() {
//        itemSize = collectionView?.frame.size
        itemSize = UIScreen.mainScreen().bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
}
